﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ribbon.Core.Graphics
{
    internal static class GraphicHelper
    {
        internal static System.Drawing.Graphics Graphics;

        internal static BufferedGraphics Buffer;

        internal static void CreateGraphics(Control con)
        {
            if (Buffer == null)
            {
                BufferedGraphicsContext context = BufferedGraphicsManager.Current;

                int width = 0;
                int height = 0;

                foreach (Screen item in Screen.AllScreens)
                {
                    if (width < item.Bounds.Width)
                        width = item.Bounds.Width;
                    if (height < item.Bounds.Height)
                        height = item.Bounds.Height;
                }

                Buffer = context.Allocate(con.CreateGraphics(), new Rectangle(0, 0, width, height));

                Graphics = Buffer.Graphics;

                Graphics.SmoothingMode = SmoothingMode.AntiAlias;

                Graphics.SmoothingMode = SmoothingMode.HighQuality;

                Graphics.CompositingQuality = CompositingQuality.HighQuality;

                Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

                //Graphics.SmoothingMode = SmoothingMode.AntiAlias;//最高图片平滑算法
                //Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;//最高质量图像插值算法
                //Graphics.CompositingQuality = CompositingQuality.AssumeLinear;//最高质量图像合成算法
                //Graphics.PixelOffsetMode = PixelOffsetMode.Half;//最高质量像素偏移
                //Graphics.CompositingMode = CompositingMode.SourceOver;//图片合成模式覆盖 不控制图片质量
            }
        }

        internal static void Dispose()
        {
            if (Graphics != null)
            {
                using (Graphics)
                {
                    Graphics = null;
                }
            }

            if (Buffer != null)
            {
                using (Buffer)
                {
                    Buffer = null;
                }
            }
        }
    }
}
